package com.zry.traced.presenter

import android.app.AlertDialog
import android.content.Intent
import com.zry.traced.R
import com.zry.traced.adapter.OutTreasuryAdapter
import com.zry.traced.adapter.PackingOperationAdapter
import com.zry.traced.adapter.PutStorageAdapter
import com.zry.traced.animation.RecyclerViewUtilKt
import com.zry.traced.greendao.entity.OutTreasuryCode
import com.zry.traced.greendao.entity.PackingOperationInfo
import com.zry.traced.greendao.entity.PutStorageCode
import com.zry.traced.retrofit.BaseApiRetrofit
import com.zry.traced.ui.activity.LocalDataListActivity
import com.zry.traced.ui.activity.SubmitPutStorageActivity
import com.zry.traced.update.UpdateAppService
import kotlinx.android.synthetic.main.activity_local_data_list.*

class LocalDataListPresenter(private val activity: LocalDataListActivity) {
    var recyclerViewUtilKt: RecyclerViewUtilKt? = null
    fun initRecycler() {
        recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.localDataRecycler)
        recyclerViewUtilKt?.initRecyclerView()
    }

    fun getLocalDataList() {
        when (activity.title) {
            "未上传入库记录" -> {
                val putStorageCodeList: MutableList<PutStorageCode> = activity.putStorageCodeDao!!.queryBuilder().list()
                putStorageAdapter(putStorageCodeList)
            }
            "未上传出库记录" -> {
                val outTreasuryCodeList: MutableList<OutTreasuryCode> = activity.outTreasuryCodeDao!!.queryBuilder().list()
                outTreasuryAdapter(outTreasuryCodeList)
            }
            "未上传打包记录" -> {
                val packingOperationInfoList = activity.packingOperationInfoDao!!.loadAll()
                packingOperationAdapter(packingOperationInfoList)
            }
        }
    }

    private fun putStorageAdapter(putStorageCodeList: MutableList<PutStorageCode>) {
        val putStorageAdapter = PutStorageAdapter(R.layout.item_operation_record, putStorageCodeList)
        recyclerViewUtilKt?.setAdapter(putStorageAdapter)
        putStorageAdapter.setOnItemClickListener { adapter, view, position ->
            intentMethod(putStorageCodeList[position].operationType, putStorageCodeList[position].timeID)
        }
        putStorageAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(activity)
                    .setTitle("删除记录")
                    . setMessage("您确定要删除此记录吗？")
                    .setPositiveButton("删除") { dialog, which ->
                        activity.putStorageCodeDao!!.deleteByKey(putStorageCodeList[position].timeID)
                        putStorageCodeList.removeAt(position)
                        putStorageAdapter.notifyDataSetChanged()
                        dialog.dismiss()
                    }.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                    .setCancelable(false)?.create()?.show()

            return@setOnItemLongClickListener true
        }
    }

    private fun outTreasuryAdapter(outTreasuryCodeList: MutableList<OutTreasuryCode>) {
        val outTreasuryAdapter = OutTreasuryAdapter(R.layout.item_operation_record, outTreasuryCodeList)
        recyclerViewUtilKt?.setAdapter(outTreasuryAdapter)
        outTreasuryAdapter.setOnItemClickListener { adapter, view, position ->
            intentMethod(outTreasuryCodeList[position].operationType, outTreasuryCodeList[position].timeID)
        }
        outTreasuryAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(activity)
                    .setTitle("删除记录")
                    . setMessage("您确定要删除此记录吗？")
                    .setPositiveButton("删除") { dialog, which ->
                        activity.outTreasuryCodeDao!!.deleteByKey(outTreasuryCodeList[position].timeID)
                        outTreasuryCodeList.removeAt(position)
                        outTreasuryAdapter.notifyDataSetChanged()
                        dialog.dismiss()
                    }.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                    .setCancelable(false)?.create()?.show()

            return@setOnItemLongClickListener true
        }
    }

    private fun packingOperationAdapter(packingOperationInfoList: MutableList<PackingOperationInfo>) {
        val packingOperationAdapter = PackingOperationAdapter(R.layout.item_operation_record, packingOperationInfoList)
        recyclerViewUtilKt?.setAdapter(packingOperationAdapter)
        packingOperationAdapter.setOnItemClickListener { adapter, view, position ->
            intentMethod(packingOperationInfoList[position].operationType, packingOperationInfoList[position].timeID)
        }
        packingOperationAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(activity)
                    .setTitle("删除记录")
                    . setMessage("您确定要删除此记录吗？")
                    .setPositiveButton("删除") { dialog, which ->
                        activity.packingOperationInfoDao!!.deleteByKey(packingOperationInfoList[position].timeID)
                        packingOperationInfoList.removeAt(position)
                        packingOperationAdapter.notifyDataSetChanged()
                        dialog.dismiss()
                    }.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                    .setCancelable(false)?.create()?.show()

            return@setOnItemLongClickListener true
        }
    }

    private fun intentMethod(title: String, TimeID: String) {
        val intent = Intent(activity, SubmitPutStorageActivity::class.java)
        intent.putExtra("title", title)
        intent.putExtra("TimeID", TimeID)
        activity.startActivity(intent)
    }
}